package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.InputtipsQuery;
import com.amap.api.services.help.Tip;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RideRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.WalkRouteResult;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class InPOISearchActivity extends BaseActivity implements PoiSearch.OnPoiSearchListener {


    private EditText indata;
    private ListView poilistview;
    private SharedPreferences sharedPreferences;
    private String city;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_in_poisearch);
        sharedPreferences = getApplicationContext().getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        city = sharedPreferences.getString("city", "");
        indata = (EditText) findViewById(R.id.poi_inputdata);
        poilistview = (ListView) findViewById(R.id.poi_listview);
        indata.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 当内容变化是 调用poi 搜索
                getPOIData(String.valueOf(s));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
        // 获取poi 搜索数据
    private void getPOIData(String s) {
        PoiSearch.Query query = new PoiSearch.Query(s, "", "");
        query.setPageSize(15);
        PoiSearch search = new PoiSearch(this,query);
        search.setOnPoiSearchListener(this);
        search.searchPOIAsyn();
    }

    List<HashMap<String,String>>slist= new ArrayList();
    HashMap<String,String>map;
    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {
        ArrayList<PoiItem> pois = poiResult.getPois();
        for (int j = 0; j < pois.size(); j++) {
            String title = pois.get(j).getTitle();
            String snippet = pois.get(j).getSnippet();
            LatLonPoint latLonPoint = pois.get(j).getLatLonPoint();
            double latitude = latLonPoint.getLatitude();
            double longitude = latLonPoint.getLongitude();
        }
        final PoiSearchAdapter adapter = new PoiSearchAdapter(this,pois);
        poilistview.setAdapter(adapter);
        poilistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                indata.setText(adapter.pois.get(position).getTitle());
                double latitude = adapter.pois.get(position).getLatLonPoint().getLatitude();
                double longitude = adapter.pois.get(position).getLatLonPoint().getLongitude();
                Intent intent = new Intent();
                intent.putExtra("latitude",String.valueOf(latitude));
                intent.putExtra("longitude",String.valueOf(longitude));
                intent.putExtra("snippet",adapter.pois.get(position).getSnippet());

                InPOISearchActivity.this.setResult(10,intent);
                finish();

            }
        });
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {

    }

}
